package com.ffs.template.util;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.util.Random;

/**
 * 程序关闭执行
 *
 * @author ffs80
 */
public class JVMClose extends Thread {

    @Override
    public void run() {
        endOut();
    }

    /**
     * 程序结束输出
     */
    private void endOut() {
        DocumentBuilder builder;
        try {
            /**
             * 随机从文件中选取一条语句输出
             */
            InputStream config = this.getClass().getClassLoader().getResourceAsStream("end.xml");
            Random random = new Random();
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = builder.parse(config);
            NodeList nodeList = document.getElementsByTagName("sentence");
            int index = random.nextInt(nodeList.getLength());
            String content = nodeList.item(index).getTextContent();
            System.out.println(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
